package com.huaboot.system.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.huaboot.commont.core.BaseResponse;
import com.huaboot.commont.core.ResultUtils;
import com.huaboot.system.convert.SysPostConvert;
import com.huaboot.system.entity.SysPostEntity;
import com.huaboot.system.service.SysPostService;
import com.huaboot.system.query.SysPostQuery;
import com.huaboot.system.vo.SysPostVO;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.huaboot.commont.page.PageResult;
import java.util.List;

/**
* 岗位管理
*
* @author ff 222
* @since 1.0.0 2023-08-29
*/
@RestController
@RequestMapping("sys/post")
@Tag(name="岗位管理")
@AllArgsConstructor
public class SysPostController {
    private final SysPostService sysPostService;

    @GetMapping("list")
    @Operation(summary = "列表")
    public BaseResponse<List<SysPostVO>> list() {
        List<SysPostVO> list = sysPostService.getList();

        return ResultUtils.success(list);
    }

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('system:post:page')")
    public BaseResponse<PageResult<SysPostVO>> page(SysPostQuery query){
        return ResultUtils.success(sysPostService.page(query));
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('system:post:info')")
    public BaseResponse<SysPostVO> get(@PathVariable("id") Long id){
        SysPostEntity entity = sysPostService.getById(id);

        return ResultUtils.success(SysPostConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('system:post:save')")
    public BaseResponse<?> save(@RequestBody SysPostVO vo){
        sysPostService.save(vo);

        return ResultUtils.success("新增成功");
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('system:post:update')")
    public BaseResponse<?> update(@RequestBody  SysPostVO vo){
        sysPostService.update(vo);

        return ResultUtils.success("修改成功");
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('system:post:delete')")
    public BaseResponse<?> delete(@RequestBody List<Long> idList){
        sysPostService.delete(idList);

        return ResultUtils.success("删除成功");
    }
}
